package com.ccl.iot.device;

import com.ccl.iot.R;
import com.ccl.iot.object.IOTDeviceInfoEx;

/**
 * 创建者     王浩
 * 创建时间   2017/5/31 10:21
 * 描述	      ${TODO}
 */

public class IOTAirCondition25 extends IOTAirCondition {

    public static final int POWER_STATE = 0;    //电源状态
    public static final int EAC_MODE = 0;        //运行模式
    public static final int EAC_SPEED = 1;    //风速
    public static final int EAC_DORECTOON = 1;    //风向
    public static final int SET_TEMP = 2;        //设置温度
    public static final int REAL_TEMP = 3;    //环境实际温度

    public IOTAirCondition25(long aDevID, IOTDeviceInfoEx aDeviceInfo, int aStateAddress, int aStateSize) {
        super(aDevID, aDeviceInfo, aStateAddress, aStateSize);
    }

    @Override
    public boolean isOn() {
        if (mRegState != null) {
            int turn = mRegState[POWER_STATE] & 14;
            return turn > 0 ? true : false;
        }
        return false;
    }

    @Override
    public void turn(boolean turn) {
        byte b = (byte) ((byte) (turn ? 1 : 0) << 2);
        b = (byte) (b | (mRegState[POWER_STATE] & 243));
        iRegisterInterface.writeRegister(this, getRegisterAddress(), 1, new byte[]{b}, 2);
    }

    @Override
    public boolean isOnline() {
        if (mRegState != null) {
            int online = mRegState[POWER_STATE] & 3;
            return online > 0 ? true : false;
        }
        return false;
    }

    @Override
    public void addTemp() {
        byte temp = mRegState[SET_TEMP];
        if (temp < 32) {
            iRegisterInterface.writeRegister(this, getRegisterAddress() + SET_TEMP, 1, new byte[]{++temp}, 2);
        }
    }

    @Override
    public void reduceTemp() {
        byte temp = mRegState[SET_TEMP];
        if (temp > 16) {
            iRegisterInterface.writeRegister(this, getRegisterAddress() + SET_TEMP, 1, new byte[]{--temp}, 2);
        }
    }

    @Override
    public void setTemp(int temp) {
        temp = temp > 32 ? 32 : temp;
        temp = temp < 16 ? 16 : temp;
        iRegisterInterface.writeRegister(this, getRegisterAddress() + SET_TEMP, 1, new byte[]{(byte) temp}, 2);
    }

    @Override
    public void setMode(int mode) {
        mode = mode > 5 ? 5 : mode;
        mode = mode < 0 ? 0 : mode;
        mode = (mode << 4) | (mRegState[EAC_MODE] & 0xf);
        iRegisterInterface.writeRegister(this, getRegisterAddress() + EAC_MODE, 1, new byte[]{(byte) mode}, 2);
    }

    @Override
    public void setSpeed(int speed) {
        speed = speed > 3 ? 3 : speed;
        speed = speed < 0 ? 0 : speed;
        speed = speed | (mRegState[EAC_SPEED] & 0xf0);
        iRegisterInterface.writeRegister(this, getRegisterAddress() + EAC_SPEED, 1, new byte[]{(byte) speed}, 2);
    }

    @Override
    public void setDirection(int direction) {
        direction = direction > 10 ? 10 : direction;
        direction = direction < 0 ? 0 : direction;
        direction = (direction << 4) | (mRegState[EAC_DORECTOON] & 0xf);
        iRegisterInterface.writeRegister(this, getRegisterAddress() + EAC_DORECTOON, 1, new byte[]{(byte) direction}, 2);
    }

    @Override
    public void modifyMode() {
        byte mode = (byte) getMode();
        mode = (byte) (++mode % 6);
        mode = (byte) ((((mode == 0) ? 2 : mode) << 4) | (mRegState[EAC_MODE] & 0xf));
        iRegisterInterface.writeRegister(this, getRegisterAddress() + EAC_MODE, 1, new byte[]{mode}, 2);
    }

    @Override
    public void modifySpeed() {
        int speed = getSpeed();
        speed = (byte) (++speed % 4);
        speed = (speed == 0 ? 1 : speed) | (mRegState[EAC_SPEED] & 0xf0);
        iRegisterInterface.writeRegister(this, getRegisterAddress() + EAC_SPEED, 1, new byte[]{(byte) speed}, 2);
    }

    @Override
    public void modifyDirection() {
        int direction = getDirection();
        direction = (byte) (++direction % 10);
        direction = (direction == 0 ? 1 : direction) | (mRegState[EAC_DORECTOON] & 0xf);
        iRegisterInterface.writeRegister(this, getRegisterAddress() + EAC_DORECTOON, 1, new byte[]{(byte) direction}, 2);
    }

    @Override
    public int getMode() {
        byte mode = (byte) (mRegState[EAC_MODE] >> 4);
        return mode;
    }

    @Override
    public int getSpeed() {
        return mRegState[EAC_SPEED] & 0xf;
    }

    @Override
    public int getDirection() {
        return mRegState[EAC_DORECTOON] >> 4;
    }

    @Override
    public int getTemp() {
        return mRegState[SET_TEMP];
    }

    @Override
    public int getRealTemp() {
        return mRegState[REAL_TEMP];
    }

    @Override
    public int getRegisterAddress() {
        return super.getRegisterAddress() + 26;
    }

    @Override
    public int getCornerId() {
        return R.drawable.icon_snowflake;
    }
}
